package itsm.isperp.module.service.configuration;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.configuration.ConfCategoryProperty;
import itsm.isperp.module.repository.configuration.ConfCategoryPropertyMapper;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 配置分类属性定义服务
 * 
 * @author lizx
 * @date 2013-4-29
 */
@Service
public class ConfCategoryPropertyService extends
		BaseService<ConfCategoryProperty> {

	@Autowired
	ConfCategoryPropertyMapper confCategoryPropertyMapper;

	@Override
	public ConfCategoryPropertyMapper getRepository() {
		return confCategoryPropertyMapper;
	}

	/**
	 * 查询指定分类的属性（针对当前分类的属性）
	 * 
	 * @return
	 */
	public List<ConfCategoryProperty> findProperty(String categoryId) {
		DataRequest req = new JqGridRequest();
		req.isPageable(false);
		// req.addFilterRule("confCategoryId", JqGridFilterRuleOp.eq,
		// categoryId); // 编号
		return confCategoryPropertyMapper
				.findAllPropertyByCategoryId2(categoryId);
	}

	/**
	 * 查询指定分类的属性（针对当前分类以及其父级分类的属性）
	 * 
	 * @return
	 */
	public List<Map<String, Object>> findAllProperty(String categoryId) {

		return confCategoryPropertyMapper
				.findAllPropertyByCategoryId(categoryId);
	}

	public List<Map<String, Object>> findAllProperty(String categoryId,
			String tecServiceId) {

		String sql = "SELECT a.*,b.name AS parent_name,b.treenode_name_path AS "
				+ " parent_treenode_name_path FROM conf_category_property a JOIN"
				+ " conf_category b ON a.conf_category_id=b.id"
				+ " WHERE a.id in(select CONF_CI_PROPERTY_ID from TEC_SERVICE_RELATE_CONF k1 left join tec_service_relate_ci_category b on k1.tec_relate_ci_category_id=b.id where TYPE='扩展' AND ( (SELECT c.treenode_path FROM conf_category c WHERE c.id='"
				+ categoryId
				+ "') LIKE CONCAT(CONCAT('%',b.ci_category_id),'%')) and k1.TEC_SERVICE_ID in ("
				+ tecServiceId
				+ ")) and ("
				+ " (select c.treenode_path from conf_category c where c.id='"
				+ categoryId
				+ "') "
				+ " LIKE"
				+ " CONCAT(CONCAT('%',a.conf_category_id),'%')) ORDER BY a.order_num ASC";
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		return jdbcTemplate.queryForList(sql);
		/*
		 * return confCategoryPropertyMapper
		 * .findAllPropertyByCategoryIdAndTecServiceId(categoryId,tecServiceId);
		 */
	}

	public int getPropertyCountByCategoryPath(String categoryPath) {
		if (StringUtils.isEmpty(categoryPath)) {
			return 0;
		}

		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addSelectColumns("COUNT(1) AS count_t");
		req.addFilterRule("treenode_path", JqGridFilterRuleOp.bw, categoryPath);

		return Integer.parseInt(this.getRepository()
				.findAllDetailByRequest(req).getContent().get(0).get("count_t")
				.toString());
	}

	/**
	 * 根据配置项分类删除其其自定义属性（包括其对应的资产的自定义属性值）
	 * 
	 * @param categoryId
	 *            配置项分类ID
	 * @return void
	 */
	public void deleteCategoryPropertyByCategoryPath(String categoryPath) {
		String sql = "DELETE FROM conf_category_property WHERE  conf_category_id  IN (SELECT id FROM conf_category WHERE treenode_path LIKE  '"
				+ categoryPath + "%' )";
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		jdbcTemplate.update(sql);
	}
	
	 public List<ConfCategoryProperty> findAllPropertyEx(String categoryId) {

	    return confCategoryPropertyMapper
	        .findAllPropertyByCategoryIdEx(categoryId);
	  }
}
